/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;

/**
 *
 * @author satria.sp
 */
public class setNewPassword {
    public static void main(String[] args) {
        Properties prop = new Properties();
        String[] attrIDs = {"cn", "distinguishedName", "sn", "givenname", "mail", "telephonenumber", "userPrincipalName"};
        String searchBase = "dc=serper,dc=com"; 
        String userName = "satria@serper.com";  
        try { 
            prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//            prop.put(Context.SECURITY_AUTHENTICATION, "Simple");
            prop.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5");
            prop.put(Context.REFERRAL, "follow");
            prop.put(Context.SECURITY_PRINCIPAL, "administrator@serper.com");
            prop.put(Context.SECURITY_CREDENTIALS, "Bismillah143");
            prop.put(Context.PROVIDER_URL, "ldap://192.168.88.70:389/dc=serper,dc=com");
            
            InitialDirContext context = new InitialDirContext(prop);
            
            //set password is a ldap modfy operation  
            ModificationItem[] mods = new ModificationItem[2];  
   
            //Replace the "unicdodePwd" attribute with a new value  
            //Password must be both Unicode and a quoted string  
            String oldQuotedPassword = "\"PanjulTE143\"";  
            byte[] oldUnicodePassword = oldQuotedPassword.getBytes("UTF-16LE");  
            String newQuotedPassword = "\"PanjulTE1433\"";  
            byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");  
          
            mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("unicodePwd", oldUnicodePassword));  
            mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));  
   
            // Perform the update  
            context.modifyAttributes(userName + searchBase, mods);  
            System.out.println(userName + " " + mods);  
          
            System.out.println("Reset Password for: " + userName);    
            context.close();  
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
